﻿using System;
using FrameWork.MongoDB;
using FrameWork.MongoDB.MongoDbConfig;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Driver;
using Newtonsoft.Json;

namespace MongoDBDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                var database = "CustomLogging";
                var collection = "DefaultLog";
                var dbstring =
                    "mongodb://root:Aa111111@dds-uf6d070b664cdf641.mongodb.rds.aliyuncs.com:3717,dds-uf6d070b664cdf642.mongodb.rds.aliyuncs.com:3717/admin?replicaSet=mgset-3135387";
                var db = new MongoClient("mongodb://root:Aa111111@dds-uf6d070b664cdf641.mongodb.rds.aliyuncs.com:3717,dds-uf6d070b664cdf642.mongodb.rds.aliyuncs.com:3717/admin?replicaSet=mgset-3135387").GetDatabase(database);
                /*
                             var coll = db.GetCollection<TestMongo>(collection);

                             var entity = new TestMongo
                             {
                                 Name = "SkyChen",
                                 Amount = 100,
                                 CreateDateTime = DateTime.Now
                             };

                             coll.InsertOne(entity);*/
                //coll.InsertOneAsync(entity).ConfigureAwait(false);

                var entity = new TestMongo
                {
                    Name = "SkyChen",
                    Amount = 100,
                    CreateDateTime = DateTime.Now
                };

                //var localLog = NLog.LogManager.GetCurrentClassLogger();

                //localLog.Info(BuildContent(entity));

                MongoDbService mongoDbService = new MongoDbService(dbstring, database, collection);

                var coll = db.GetCollection<LogModel>(collection);
                var find = coll.Find(item => item.Message.Contains("930e0d22-b7a0-49f3-832e-be84cf27d10d"));

                var logs = find.ToList();//mongoDbService.PageList<LogModel>(item => item.Logger == "actionLog", null);
                foreach (var logModel in logs)
                {
                    Console.WriteLine(logModel.Date);
                    Console.WriteLine(logModel.Level);
                    //Console.WriteLine(logModel.Message);
                    Console.WriteLine(logModel.Logger);
                    Console.WriteLine(logModel.Properties.ThreadID);
                    Console.WriteLine(logModel.Properties.ProcessID);
                    Console.WriteLine(logModel.Properties.ProcessName);
                    Console.WriteLine(logModel.Properties.UserName);
                }
                //Console.WriteLine(log.TotalPage);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
            }

            Console.Read();
        }

        private static dynamic BuildContent(dynamic content)
        {
            if (content.GetType().IsClass == true && content.GetType().Name != "String" && content.GetType().Name != "Int32")
            {
                return JsonConvert.SerializeObject(content);
            }

            return content;
        }
    }


    public class TestMongo : MongoEntity
    {

        [BsonDateTimeOptions(Kind = DateTimeKind.Local)]
        public DateTime CreateDateTime { get; set; }

        public decimal Amount { get; set; }

        public string Name { get; set; }
    }

    public class LogModel : MongoEntity
    {
        public DateTime Date { get; set; }
        public string Level { get; set; }
        public string Message { get; set; }
        public string Logger { get; set; }

        public PropertiesModel Properties { get; set; }
    }

    public class PropertiesModel
    {
        public int ThreadID { get; set; }
        public int ProcessID { get; set; }
        public string ProcessName { get; set; }
        public string UserName { get; set; }
    }
}
